package com.szh.te.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szh.te.entity.Account;
import com.szh.te.mapper.AccountMapper;
import com.szh.te.service.AccountService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 虎哥
 */
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    @GlobalTransactional
    public void deduct(String userId, int money) {
        log.info("开始扣款");
        
        int deduct = accountMapper.deduct(userId, money);
        if (deduct <0){
            throw new RuntimeException("扣款失败，可能是余额不足！");
        }
        
        log.info("扣款成功");
    }
}
